package com.ai.bitcoin.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 
 * @author xd
 *
 */
public abstract class ClassUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);
	
	/**
	 * 
	 * @param name
	 * @param object
	 * @return
	 */
	public static Object getFieldValueByName(String name, Object object) {
		Object value = null;
		try {
			String firstLetter = name.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + name.substring(1);
			Method method = object.getClass().getMethod(getter, new Class[] {});
			value = method.invoke(object, new Object[] {});
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return value;
	}
	
	/**
	 * 
	 * @param object
	 * @return
	 */
	public static String[] getFiledName(Object object) {
		Field[] fields = object.getClass().getDeclaredFields();
		String[] fieldNames = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			fieldNames[i] = fields[i].getName();
		}
		return fieldNames;
	}

	public static class T {
		private String a;
		private String b;
		private Boolean c;
		
		public T(){}
		public T(String a,String b,Boolean c) {
			this.a = a;
			this.b = b;
			this.c = c;
		}
		
		public String getA() {
			return a;
		}
		public void setA(String a) {
			this.a = a;
		}
		public String getB() {
			return b;
		}
		public void setB(String b) {
			this.b = b;
		}
		public Boolean getC() {
			return c;
		}
		public void setC(Boolean c) {
			this.c = c;
		}
	}
	   
	
	public static void main(String[] args) {
		
		T t = new T("a", "b", false);
		
		String[] fieldNames = ClassUtil.getFiledName(t);
		System.out.print("size -- >"+fieldNames.length);
		
	}
	   

}
